package com.woniuxy.controller;


import com.woniuxy.client.RoomClientProduct;
import com.woniuxy.domain.Product;
import com.woniuxy.domain.Room;
import com.woniuxy.domain.Seat;
import com.woniuxy.dto.JsonResultDto;
import com.woniuxy.dto.RoomDto;
import com.woniuxy.param.ProductParam;
import com.woniuxy.param.RoomParam;
import com.woniuxy.service.RoomService;
import com.woniuxy.service.SeatService;
import com.woniuxy.service.TicketService;
import com.woniuxy.util.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyanzu
 * @since 2021-05-26
 */
@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;
    @Autowired
    private RoomClientProduct roomClientProduct;
    @Autowired
    private SeatService seatService;
    @Autowired
    private TicketService ticketService;
    @GetMapping("/selectRoom")
    public JsonResultDto selectRoom(RoomParam roomParam){
        List<Room> rooms = roomService.selectRoom(roomParam);
        List<RoomDto> roomDtos = BeanCopyUtil.copyListPropertie(rooms, RoomDto::new);
        return new JsonResultDto("1000","successful",null,roomDtos);
    }
    //根据影厅ID找到已排片的所有时间,返回该影厅的空闲时间
    //chooseStartTime选择开始时间,chooseEndTime选择结束时间
    @Transactional
    @GetMapping("/selectProductByRid")
    public JsonResultDto selectProductByRid(int rid, Date chooseStartTime, Date chooseEndTime,ProductParam productParam){
        List<Product> products = roomClientProduct.selectProductByRid(rid);
        for(Product product:products){
            if((chooseStartTime.after(product.getpStarttime())&&chooseStartTime.before(product.getpEndtime()))||(chooseEndTime.after(chooseStartTime)&&chooseEndTime.before(chooseEndTime))){
                return new JsonResultDto("1001","failed",null,null);
            }
        }
        //可以排片新增场次数据(新增场次)
        //根据m_id获得该场次数量,并且排片就+1操作redis数据库(已实现)
        roomClientProduct.insertProduct(productParam);

        //根据该影厅查询座位数据
        List<Seat> seats = seatService.selectSeatByRid(rid);
        //根据该影厅的座位,(x,y)给ticket表新增数据
        for(Seat seat:seats){
            //新增票务数据
            ticketService.insertTicketBySeat(seat);
        }
        return new JsonResultDto("1002","can",null,null);
    }
}

